import Vue from 'vue'
import VueRouter from 'vue-router'
//  登录页面组件
// import Login from '../components/Login.vue'

// //  后台主页组件
// import Home from '../components/Home.vue'
// //  后台默认欢迎页面
// import Welcome from '../components/Welcome.vue'
// import user from '../components/user/user.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'
// import goods from '../components/goods/goods.vue'
// import add from '../components/goods/addgood.vue'
// import Order from '../components/order/Order.vue'
// import Reports from '../components/reports/reports.vue'
const Login = () => import('../components/Login.vue')
const Home = () => import('../components/Home.vue')
const Welcome = () => import('../components/Welcome.vue')
const user = () => import('../components/user/user.vue')
const Rights = () => import('../components/power/Rights.vue')
const Roles = () => import('../components/power/Roles.vue')
const Cate = () => import('../components/goods/Cate.vue')
const Params = () => import('../components/goods/Params.vue')
const goods = () => import('../components/goods/goods.vue')
const add = () => import('../components/goods/addgood.vue')
const Order = () => import('../components/order/Order.vue')
const Reports = () => import('../components/reports/reports.vue')

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    // 打开页面重定向到登录页面
    { path: '/', component: Login },
    { path: '/login', component: Login },
    { //   1
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: user },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: goods },
        { path: '/goods/add', component: add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Reports }

      ]
    }
  ]
})
//  挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //  to将要访问的地址
  //  from代表从哪个地址过来
  //  next()放行函数   以及强制跳转
  if (to.path === '/login') return next()
  //  获取token
  const tokenstr = window.sessionStorage.getItem('token')
  if (!tokenstr) return next('/login')
  next()
})
export default router
